//
//  GoodsDetailCell.m
//  JiuLe
//
//  Created by SunChao on 16/10/14.
//  Copyright © 2016年 Fine. All rights reserved.
//
#define iPhone5 ([UIScreen mainScreen].bounds.size.height == 568.0)
#define iPhone6 ([UIScreen mainScreen].bounds.size.height == 667.0)
#import "GoodsDetailCell.h"
#import "UIImageView+WebCache.h"
@implementation GoodsDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}
-(void)setModel:(GoodsDetailModel *)model
{
    _model = model ;
    _price.text = model.price;
    _discountprice.text = [NSString stringWithFormat:@"￥%@",model.discountprice];
    NSString *priceOld = [NSString stringWithFormat:@"￥%@",model.price];
    NSUInteger length = [priceOld length];
    if (length>0) {
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:priceOld];
        [attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(1, length-1)];
        [attri addAttribute:NSStrikethroughColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, length)];
        [_price setAttributedText:attri];
    }
    _goodTitle.text = model.goodTitle;
    NSString *introduce_images = model.introduceImages;
    NSArray *introArr = [introduce_images componentsSeparatedByString:@","];
    NSMutableDictionary *imgDic = [NSMutableDictionary new];
    for (int i = 0; i<introArr.count; i++) {
        NSString *str1 = [NSString stringWithFormat:@"%@",introArr[i]];
        NSArray *tmpArr1 = [str1 componentsSeparatedByString:@"_size_"];
        NSString *str2 =  [tmpArr1 objectAtIndex:1];
        NSArray *tmpArr2 = [str2 componentsSeparatedByString:@"."];
        NSString *str3 = [tmpArr2 objectAtIndex:0];
        NSArray *imgArr = [str3 componentsSeparatedByString:@"*"];
        NSString *keyString = [NSString stringWithFormat:@"%d",i];
        [imgDic setObject:imgArr forKey:keyString];
    }

    if ([model.type isEqualToString:@"3"]) {
        float discount = [model.discountprice floatValue];
        NSString *str = [NSString stringWithFormat:@" %.0f",discount];
        _discountprice.attributedText = [self setLabelText:str image:[UIImage imageNamed:@"goods_integra"]];
    }else
    {
        float discount = [model.discountprice floatValue];
        _discountprice.text = [NSString stringWithFormat:@"￥%.2f",discount];
    }
    if ([model.type isEqualToString:@"3"]) {
        self.textLabel.hidden =YES;
    }else{
        self.textLabel.text =[NSString stringWithFormat:@"可返积分:%@",model.pointsreward];
    }
    if (iPhone5) {
        self.textLabel.font = [UIFont systemFontOfSize:14];
    }
}
- (NSMutableAttributedString *)setLabelText:(NSString *)title image:(UIImage *)image{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:title];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = image;
    attach.bounds = CGRectMake(0, 0, 20, 20);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    [string insertAttributedString:attachString atIndex:0];
    return string;
}
@end
